js.underas.net.RequestManager = {
	"type":"GET"
	,"format":"json"
	,"url":""
	,"_rootUrl":""
	,"_displayLoad":true
	,"TP_JSON":"json"
	,"TP_JSONP":"jsonp"
	,"_log_erro":function(args){}
	,"_log_success":function(args){}
	,"addRequest":function(req){		

		req.url = req.url||this.url;
		if(req.rootUrl){
			req.url = req.rootUrl+req.url;
		}else{
			req.url = this._rootUrl+req.url;
		};
		req.format = req.format||this.format;
		var tsk = new js.underas.net.Task({"t":req.t,"s":req.url});
		
		if(typeof req.puid == "undefined"){
			var eleM = $$("#conteudo .windowCA");
			if(eleM.length > 0){
				req["puid"] =  eleM.get("data-modvar").toString();		
			}else{
				req["puid"] = $$("#conteudo .windowC").get("data-modvar").toString();
			};
		};
		
		if(!req["method"]){
			req["method"]="get";
		};
		
		
		var idWinMod = "#"+window[req["puid"]].getEle().get("id");
		//var idWinMod = "#"+window[req["puid"]].getEle().get("id");
		$$(idWinMod+" .blockrequest").addClass("_req_"+tsk.idRequest).setStyle("display",(this._displayLoad?"block":"none"));
		$$(idWinMod+" .blockrequest .BoxTasks").grab(tsk.getEle());
		//var req2 = {};	
		/*
		for(var key in req){
			//console.log(key);
			if(key!="onLoad"&&req[key]!=""&&key!="url"&&key!="puid"&&req[key]!="null"&&key!="method"&&key!="format"){
				req2[key]=req[key];
			}   	
		};
		*/ 		
		var onLoad = function(dtr){
					var eleM = $$("#uid_"+this.idRequest);
					if(!dtr.erro){						
						this.finalizar(dtr);						
						var paiRE = eleM.getParent(".blockrequest");				
						paiRE.removeClass("_req_"+this.idRequest);				
						var classes = paiRE.get("class");				
						if(classes.indexOf("_req_") < 0){
							//paiRE.set("").find(".Task").remove().end().find(".BoxTasks").hide();							
							paiRE.setStyle("display","none").getChildren(".BoxTasks").each(function(item){
								item.setStyle("display","none");
								item.getElement(".Task").destroy();
							});
							js.underas.net.RequestManager.finalizar();
						}				
					}else{
						this.erroState(dtr.erro);
					}		
		};		
		
		var req_c12667 = {};
		//req_c.$events.success[1]=function(dta2){console.log("artificial");console.log(dta2);console.log("fim artificial")};
		
		if(req.format=="json"){
			req_c12667 = new Request.JSON({
				url:req.url
				,"emulation":false
				,"onFailure":function(xhr){				
					var errotxt = "Unknow Error";
					if (xhr.status == 0) {
						errotxt = 'You are offline!! Please Check Your Network.';
					} else if (xhr.status == 404) {
						errotxt = 'URL not found.';
					} else if (xhr.status == 500) {
						errotxt = 'Internal Server Error.';
					}else{
						errotxt = 'Unknow Error.' + xhr.responseText;
					}				
					js.underas.net.RequestManager._log_erro({"s":"RequestManager.Erro"+"","e":[errotxt]});
				}
				,"onSuccess":onLoad.bind(tsk)
				,"onError":function(text, error){js.underas.net.RequestManager._log_erro({"s":"RequestManager.Erro"+"","e":[error]});}
			});
		}else{
			//req.data["format"]=req.format;
			req_c12667 =new Request.JSONP({"data":req.data,"url":req.url
				,"onTimeout":function(){
					js.underas.net.RequestManager._log_erro({"s":"RequestManager.Erro"+"","e":["Request Time out"]});
				}
				,"onSuccess":onLoad.bind(tsk)
				,"onError":function(text, error){js.underas.net.RequestManager._log_erro({"s":"RequestManager.Erro"+"","e":[error]});}
			});
		};	
		if(req_c12667.$events.success&&req.onLoad){
			req_c12667.$events.success[1]=req.onLoad;	
		};
			
		if(req.format=="json"){
			if(req["method"]=="put"||req["method"]=="post"){				
				//,urlEncoded: false
			
				req_c12667.headers["Content-type"] = "application/json";
				//myid2.headers["Content-type"] = "application/json";

				req_c12667.setOptions({urlEncoded:false});

				//myid2.post(JSON.stringify({cdTipoDocumento: 65, tpDocumento: "ALTMED"}));
				if(req.data){
					req_c12667[req["method"]](JSON.stringify(req.data));
				}else{
					req_c12667[req["method"]]();
				};
				
				/*
				req_c12667.send({
				    method: req["method"]
				    ,data: JSON.stringify(req2)
				});
				*/
			}else{
				if(req.data){
					req_c12667[req["method"]](req.data);
				}else{
					req_c12667[req["method"]]();
				}
				
			}
		}else{
			//if(req.rootUrl||this._rootUrl!=""){
				//delete req_c12667.headers['X-Requested-With'];
			//};
			req_c12667.send();
		};	
	}
	,"refresh":function(){} 
	,"finalizar":function(){
		//$$(event+" .BoxTasks").setStyle("display","none");
	}
	,"notifyTaskErro":function(idTask,erro){
		//.erroState()
	}
	,"showAllTasks":function(p_idWinMod){
		$$("#"+p_idWinMod+" .BoxTasks").setStyle("display","block");
	} 
	,"hideAllTasks":function(p_idWinMod){
		$$("#"+p_idWinMod+" .BoxTasks").setStyle("display","none");
	}
	,"setFormat":function(p_format){
		this.format = p_format;
	}
	,"setUrl":function(p_url){
		this.url = p_url;
	}
	,"setRootUrl":function(p_rootUrl){
		this._rootUrl = p_rootUrl;
	}	
	,"enableDisplayLoad":function(p_on){		
		this._displayLoad = p_on;
	}
 	,"removeAllTasks":function(p_idWinMod){
		if($$("#"+p_idWinMod).hasClass("erro_load")){
			//$$(event+" .Task").destroy();
			$$("#"+p_idWinMod+" .BoxTasks .TaskErro").destroy();
			$$("#"+p_idWinMod).setStyle("display","none").erase("class").addClass("blockrequest normal_load");
		}	
		//$(event).hide();
		
		this.hideAllTasks(p_idWinMod);
	}
 };
